import { defineStore } from 'pinia'
import { getOrders } from '@/api'
import { ref } from 'vue'
const useOrders = defineStore('orders', () => {
    const orders = ref([])
    const getOrdersInfo = async() =>{
        delAllOrders()
        orders.value =await getOrders()
        return orders.value
    }
    const delAllOrders = () =>{
        orders.value=[]
    }
    const getPendingNum = () =>{
        let sum = 0
        orders.value.forEach(element => {
            if(element.order_status === 0){
                sum++
            }
        });
        return sum
    }
    const getPendedNum = () =>{
        let sum = 0
        orders.value.forEach(element => {
            if(element.order_status === 1){
                sum++
            }
        });
        return sum
    }
    const getFinishedNum = () =>{
        let sum = 0
        orders.value.forEach(element => {
            if(element.order_status === 2){
                sum++
            }
        });
        return sum
    }
    return { orders, getOrdersInfo, getPendingNum, getPendedNum, getFinishedNum }
    }, {
        persist: {
        enabled: true,
        strategies: [
        {
            key: 'orders',
            storage: localStorage
        }]
    }
})
export default useOrders